/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LocalizarClienteJuridico.java
 *
 * Created on 26/04/2011, 15:20:26
 */
package localizarInserir;

import Classe.BD;
import Classe.Bairro;
import Classe.Cidade;
import Classe.ClientePJ;
import Classe.Endereco;
import Classe.Estado;
import Classe.Validacao;
import Tela.CadastroNotaFiscal;
import Tela.CadastroOrcamento;
import editar.EditarNotaFiscal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author gladson
 */
public class LocalizarClienteJuridicoInserir extends Validacao {

    /** Creates new form LocalizarClienteJuridico */
    public LocalizarClienteJuridicoInserir(int opcntt) {
        this.opcnt = opcntt;
        initComponents();
         this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
        StxtOpcLocCcliPJ.setEnabled(false);
    }
    String SelecaoCliPj;
    CadastroNotaFiscal telanf;
    CadastroOrcamento cadasorc;
    EditarNotaFiscal ednjfc;
    int opcnt;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbSelecaoCliPJ = new javax.swing.JComboBox();
        StxtOpcLocCcliPJ = new javax.swing.JFormattedTextField();
        btPesquisarCliPj = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaCliPj = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        ItxtIdCliente = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Localizar Cliente Jurídico", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel1.setText("Localizar por:");

        cbSelecaoCliPJ.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "", "Nome Fantasia", "Razão Social", "CNPJ","Insc.Estadual","Data Cadastro" }));
cbSelecaoCliPJ.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
cbSelecaoCliPJ.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        cbSelecaoCliPJItemStateChanged(evt);
    }
    });

    StxtOpcLocCcliPJ.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    btPesquisarCliPj.setText("Pesquisar");
    btPesquisarCliPj.setToolTipText("Pesquisar");
    btPesquisarCliPj.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    btPesquisarCliPj.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btPesquisarCliPjActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(StxtOpcLocCcliPJ, javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(cbSelecaoCliPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(18, 18, 18)
            .addComponent(btPesquisarCliPj)
            .addContainerGap(390, Short.MAX_VALUE))
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(cbSelecaoCliPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(StxtOpcLocCcliPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btPesquisarCliPj))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    tabelaCliPj.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Data Cadastro", "Código", "Nome Fantásia", "Razão Social", "CNPJ", "Insc.Estadual", "Contato", "Telefone"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jScrollPane1.setViewportView(tabelaCliPj);

    jPanel6.setBackground(new java.awt.Color(255, 255, 255));
    jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

    jLabel4.setFont(new java.awt.Font("Arial", 1, 11));
    jLabel4.setText("Código do Clienter: ");

    ItxtIdCliente.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

    jButton3.setText("Incluir");
    jButton3.setToolTipText("Incluir Cliente Jurídico");
    jButton3.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
    jPanel6.setLayout(jPanel6Layout);
    jPanel6Layout.setHorizontalGroup(
        jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel6Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(ItxtIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jButton3)
            .addContainerGap(498, Short.MAX_VALUE))
    );
    jPanel6Layout.setVerticalGroup(
        jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel6Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(ItxtIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jButton4.setText("Sair");
    jButton4.setToolTipText("Sair");
    jButton4.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(jButton4)
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void btPesquisarCliPjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarCliPjActionPerformed
 if (cbSelecaoCliPJ.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Selecione uma opção para consulta!!");
        } else {
            if (validarLocalizador(StxtOpcLocCcliPJ.getText())) {
        String Opcao = ((String) cbSelecaoCliPJ.getSelectedItem());
        boolean esc = false;
        
        try {
            if (Opcao.equalsIgnoreCase("Nome Fantasia")) {
                SelecaoCliPj = "NOME_FANT_CLIENTE_PJ LIKE '%";
                esc = true;
            }
            if (Opcao.equalsIgnoreCase("Razão Social")) {
                SelecaoCliPj = "RAZAO_CLIENTE_PJ LIKE '%";
                esc = true;
            }
            if (Opcao.equalsIgnoreCase("CNPJ")) {
                SelecaoCliPj = "CNPJ_CLIENTE_PJ";
                esc = false;
            }
            
            if (Opcao.equalsIgnoreCase("Insc.Estadual")) {
                SelecaoCliPj = "INSC_EST_CLIENTE_PJ LIKE '%";
                esc = true;
            }
            
            if (Opcao.equalsIgnoreCase("Data Cadastro")) {
                SelecaoCliPj = "DATA_CADAS_CLIENTE_PJ LIKE '%";
                esc = false;
            }
            
            
            
            String sql = "SELECT * FROM CLIENTE_PJ WHERE " + SelecaoCliPj + "='" + StxtOpcLocCcliPJ.getText().trim().toUpperCase() + "'";
            
            if (esc) {
                
                String sql2 = "SELECT * FROM CLIENTE_PJ WHERE " + SelecaoCliPj + "" + StxtOpcLocCcliPJ.getText().trim().toUpperCase() + "%'";
                sql = sql2;
                
            }
            
            BD banco = new BD();
            ResultSet res = banco.consultar(sql);
            
            DefaultTableModel model = (DefaultTableModel) tabelaCliPj.getModel();
            model.setNumRows(0);
            while (res.next()) {
                Object[] linha = {
                    res.getString("DATA_CADAS_CLIENTE_PJ"),
                    res.getInt("ID_CLIENTE_PJ"),
                    res.getString("NOME_FANT_CLIENTE_PJ"),
                    res.getString("RAZAO_CLIENTE_PJ"),
                    res.getString("CNPJ_CLIENTE_PJ"),
                    res.getString("INSC_EST_CLIENTE_PJ"),
                    res.getString("CONTATO_CLIENTE_PJ"),
                    res.getString("TEL_CLIENTE_PJ")};
                model.addRow(linha);
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
            }}  
        
        
        
    }//GEN-LAST:event_btPesquisarCliPjActionPerformed
    MaskFormatter mascara = new MaskFormatter();
    private void cbSelecaoCliPJItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecaoCliPJItemStateChanged
        StxtOpcLocCcliPJ.setEnabled(true);
        
        if (cbSelecaoCliPJ.getSelectedItem() == "CNPJ") {
            try {
                mascara.setMask("##.###.###/####-##");
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            mascara.setPlaceholderCharacter(' ');
            mascara.install(StxtOpcLocCcliPJ);
        } else if (cbSelecaoCliPJ.getSelectedItem() == "Data Cadastro") {
            try {
                mascara.setMask("##/##/####");
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            
            mascara.setPlaceholderCharacter(' ');
            mascara.install(StxtOpcLocCcliPJ);
        } else {
            
            mascara.uninstall();
            StxtOpcLocCcliPJ.setText("");
            
        }
        
    }//GEN-LAST:event_cbSelecaoCliPJItemStateChanged
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       if(validarNumero(ItxtIdCliente.getText())){
           
      
        if (opcnt == 1) {
            try {
                int idCliente = Integer.parseInt(ItxtIdCliente.getText().trim());
                
                String sql = "SELECT * FROM CLIENTE_PJ CLI INNER JOIN ENDERECO EN ON CLI.ID_ENDERECO=EN.ID_ENDERECO "
                        + "INNER JOIN BAIRRO BAI ON EN.ID_BAIRRO= BAI.ID_BAIRRO "
                        + "INNER JOIN CIDADE CID ON BAI.ID_CIDADE = CID.ID_CIDADE "
                        + "INNER JOIN ESTADO EST ON CID.ID_UF = EST.ID_UF WHERE CLI.ID_CLIENTE_PJ=" + idCliente;
                BD bd = new BD();                
                
                ResultSet rs = bd.consultar(sql);
                if (rs.next()) {
                    ClientePJ cli = new ClientePJ();
                    Estado est = new Estado();
                    Cidade cid = new Cidade();
                    Bairro bai = new Bairro();
                    Endereco end = new Endereco();
                    cli.setIdClientePJ(rs.getInt("ID_CLIENTE_PJ"));
                    cli.setRazaoSocialFornecedor(rs.getString("RAZAO_CLIENTE_PJ"));
                    cli.setCnpjFornecedor(rs.getString("CNPJ_CLIENTE_PJ"));
                    cli.setInscEstadualFornecedor(rs.getString("INSC_EST_CLIENTE_PJ"));
                    cli.setTelFornecedor(rs.getString("TEL_CLIENTE_PJ"));
                    cli.setComplementoFornecedor(rs.getString("COMPL_CLIENTE_PJ"));
                    cli.setNumFornecedor(rs.getInt("NUM_CLIENTE_PJ"));
                    cli.setSalaFornecedor(rs.getString("SALA_CLIENTE_PJ"));
                    est.setIdUf(rs.getString("ID_UF"));
                    cid.setNomeCidade(rs.getString("NOME_CIDADE"));
                    bai.setNomeBairro(rs.getString("NOME_BAIRRO"));
                    end.setNomeEndereco(rs.getString("NOME_ENDERECO"));
                    end.setCepEndereco(rs.getString("CEP"));
                    cid.setEstadoCidade(est);
                    bai.setCidadeBairro(cid);
                    end.setBairroEndereco(bai);
                    cli.setEndereco(end);
                    
                    telanf.preenchePJ(cli);
                    this.dispose();                    
                    
                    
                    
                    
                }else{
                    JOptionPane.showMessageDialog(rootPane, "Cliente Jurídico não encontrado na base de dados");
                    ItxtIdCliente.setText("");
                } 
                
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            
            
            
        } else  if (opcnt == 2) {
            try {
                int idCliente = Integer.parseInt(ItxtIdCliente.getText().trim());
                
                String sql = "SELECT * FROM CLIENTE_PJ CLI INNER JOIN ENDERECO EN ON CLI.ID_ENDERECO=EN.ID_ENDERECO "
                        + "INNER JOIN BAIRRO BAI ON EN.ID_BAIRRO= BAI.ID_BAIRRO "
                        + "INNER JOIN CIDADE CID ON BAI.ID_CIDADE = CID.ID_CIDADE "
                        + "INNER JOIN ESTADO EST ON CID.ID_UF = EST.ID_UF WHERE CLI.ID_CLIENTE_PJ=" + idCliente;
                BD bd = new BD();                
                
                ResultSet rs = bd.consultar(sql);
                if (rs.next()) {
                    ClientePJ cli = new ClientePJ();
                    Estado est = new Estado();
                    Cidade cid = new Cidade();
                    Bairro bai = new Bairro();
                    Endereco end = new Endereco();
                    cli.setIdClientePJ(rs.getInt("ID_CLIENTE_PJ"));
                    cli.setRazaoSocialFornecedor(rs.getString("RAZAO_CLIENTE_PJ"));
                    cli.setCnpjFornecedor(rs.getString("CNPJ_CLIENTE_PJ"));
                    cli.setInscEstadualFornecedor(rs.getString("INSC_EST_CLIENTE_PJ"));
                    cli.setTelFornecedor(rs.getString("TEL_CLIENTE_PJ"));
                    cli.setContatoFornecedor(rs.getString("CONTATO_CLIENTE_PJ"));
                    cli.setComplementoFornecedor(rs.getString("COMPL_CLIENTE_PJ"));
                    cli.setNumFornecedor(rs.getInt("NUM_CLIENTE_PJ"));
                    cli.setSalaFornecedor(rs.getString("SALA_CLIENTE_PJ"));
                    est.setIdUf(rs.getString("ID_UF"));
                    cid.setNomeCidade(rs.getString("NOME_CIDADE"));
                    bai.setNomeBairro(rs.getString("NOME_BAIRRO"));
                    end.setNomeEndereco(rs.getString("NOME_ENDERECO"));
                    end.setCepEndereco(rs.getString("CEP"));
                    cid.setEstadoCidade(est);
                    bai.setCidadeBairro(cid);
                    end.setBairroEndereco(bai);
                    cli.setEndereco(end);
                    
                    cadasorc.preencherOrcamentoPJ(cli);
                    this.dispose();                    
                    
                    
                    
                    
                }else{
                    JOptionPane.showMessageDialog(rootPane, "Cliente Jurídico não encontrado na base de dados");
                    ItxtIdCliente.setText("");
                } 
                
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
              
        }else if(opcnt == 3){
            try {
                int idCliente = Integer.parseInt(ItxtIdCliente.getText().trim());
                
                String sql = "SELECT * FROM CLIENTE_PJ CLI INNER JOIN ENDERECO EN ON CLI.ID_ENDERECO=EN.ID_ENDERECO "
                        + "INNER JOIN BAIRRO BAI ON EN.ID_BAIRRO= BAI.ID_BAIRRO "
                        + "INNER JOIN CIDADE CID ON BAI.ID_CIDADE = CID.ID_CIDADE "
                        + "INNER JOIN ESTADO EST ON CID.ID_UF = EST.ID_UF WHERE CLI.ID_CLIENTE_PJ=" + idCliente;
                BD bd = new BD();                
                
                ResultSet rs = bd.consultar(sql);
                if (rs.next()) {
                    ClientePJ cli = new ClientePJ();
                    Estado est = new Estado();
                    Cidade cid = new Cidade();
                    Bairro bai = new Bairro();
                    Endereco end = new Endereco();
                    cli.setIdClientePJ(rs.getInt("ID_CLIENTE_PJ"));
                    cli.setRazaoSocialFornecedor(rs.getString("RAZAO_CLIENTE_PJ"));
                    cli.setCnpjFornecedor(rs.getString("CNPJ_CLIENTE_PJ"));
                    cli.setInscEstadualFornecedor(rs.getString("INSC_EST_CLIENTE_PJ"));
                    cli.setTelFornecedor(rs.getString("TEL_CLIENTE_PJ"));
                    cli.setContatoFornecedor(rs.getString("CONTATO_CLIENTE_PJ"));
                    cli.setComplementoFornecedor(rs.getString("COMPL_CLIENTE_PJ"));
                    cli.setNumFornecedor(rs.getInt("NUM_CLIENTE_PJ"));
                    cli.setSalaFornecedor(rs.getString("SALA_CLIENTE_PJ"));
                    est.setIdUf(rs.getString("ID_UF"));
                    cid.setNomeCidade(rs.getString("NOME_CIDADE"));
                    bai.setNomeBairro(rs.getString("NOME_BAIRRO"));
                    end.setNomeEndereco(rs.getString("NOME_ENDERECO"));
                    end.setCepEndereco(rs.getString("CEP"));
                    cid.setEstadoCidade(est);
                    bai.setCidadeBairro(cid);
                    end.setBairroEndereco(bai);
                    cli.setEndereco(end);
                    
                    ednjfc.preenchePJ(cli);
                    this.dispose();                    
                    
                    
                    
                    
                }else{
                    JOptionPane.showMessageDialog(rootPane, "Cliente Jurídico não encontrado na base de dados");
                    ItxtIdCliente.setText("");
                } 
                
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarClienteJuridicoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
       }
    }//GEN-LAST:event_jButton3ActionPerformed
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.dispose();
}//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
//                new LocalizarClienteJuridico().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ItxtIdCliente;
    private javax.swing.JFormattedTextField StxtOpcLocCcliPJ;
    private javax.swing.JButton btPesquisarCliPj;
    private javax.swing.JComboBox cbSelecaoCliPJ;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaCliPj;
    // End of variables declaration//GEN-END:variables

    public void recebenf(CadastroNotaFiscal njfc) {
        this.telanf = njfc;
    }
     public void recebeORC(CadastroOrcamento cadasorc) {
        this.cadasorc = cadasorc;
    }
     
     public void recebeEDnf(EditarNotaFiscal ednjfc) {
        this.ednjfc = ednjfc;
    }
}
